﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing;
using System.Threading;
using System.IO;

namespace AAPCVM
{
    public class PrintHandler
    {
        private static Candidate[] _Candidates;
        static bool _wait;
        static int linesPrinted = 0;
        private static string[] Lines;
        private static string _textTobePrinted;
        static System.IO.StreamReader fileToPrint;
        static System.Drawing.Font printFont;

        public static void PrintStreamReaderData(StreamReader reader)
        {
            PrintDocument printDocument = new PrintDocument();
            printDocument.PrintPage += new PrintPageEventHandler(OnPrintStreamReader);
            fileToPrint = reader;
            printFont = new System.Drawing.Font("Arial", 10);
            linesPrinted = 0;
            printDocument.Print();
            fileToPrint.Close();
        }
        private static void OnPrintStreamReader(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            float yPos = 0f;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;
            float linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
            while (count < linesPerPage)
            {
                line = fileToPrint.ReadLine();
                if (line == null)
                {
                    break;
                }
                yPos = topMargin + count * printFont.GetHeight(e.Graphics);
                e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
                count++;
            }
            if (line != null)
            {
                e.HasMorePages = true;
            }
        }

        public static bool PrintText(string textTobePrinted)
        {
            PrintDocument myDocument = new PrintDocument();
            myDocument.BeginPrint +=new PrintEventHandler(OnBeginPrint);
            myDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
            //myDocument.EndPrint += new PrintEventHandler();
            myDocument.DefaultPageSettings.PaperSize.RawKind = 2;
            myDocument.PrinterSettings.DefaultPageSettings.PaperSize.RawKind = 70;
            myDocument.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("A6", 413, 583);
            _textTobePrinted = textTobePrinted;
            _wait = true;
            linesPrinted = 0;
            myDocument.Print();

            for (int i = 0; (i < 10) && _wait; i++)
            {
                Thread.Sleep(1000);
            }
            if (_wait)
                return false;
            else
                return true;
        }

        private static void OnBeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            char[] param = { '\n' };

            Lines = _textTobePrinted.Split(param);
            

            int i = 0;
            char[] trimParam = { '\r' };
            foreach (string s in Lines)
            {
                Lines[i++] = s.TrimEnd(trimParam);
            }
        }

        private static void OnPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int x = e.MarginBounds.Left;
            int y = e.MarginBounds.Top;
            Font myFonts = new Font("Vanada", 12);
            Brush brush = new SolidBrush(Color.Black);

            while (linesPrinted < Lines.Length)
            {
                e.Graphics.DrawString(Lines[linesPrinted++], myFonts, brush, x, y);
                y += 15;
                if (y >= e.MarginBounds.Bottom)
                {
                    e.HasMorePages = true;
                    return;
                }
                else
                {
                    e.HasMorePages = false;
                }
            }
        }

        public static bool PrintSelection(Candidate[] Candidates)
        {
            PrintDocument myDocument = new PrintDocument();
            myDocument.PrintPage += new PrintPageEventHandler(myDocument_PrintPage);
            myDocument.EndPrint += new PrintEventHandler(myDocument_EndPrint);
            myDocument.DefaultPageSettings.PaperSize.RawKind = 2;
            

            //_wait = true;
            _Candidates = Candidates;
            linesPrinted = 0;
            myDocument.Print();

            for (int i = 0; (i < Candidates.Length) && _wait; i++)
            {
                Thread.Sleep(1000);
            }
            if (_wait)
                return false;
            else
                return true;
        }

        static void myDocument_EndPrint(object sender, PrintEventArgs e)
        {
            if (e.Cancel == false)
            {
                _wait = false;
            }
        }

        static void myDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            using (Font f = new Font("Courier New", 14, FontStyle.Bold))
            using (SolidBrush br = new SolidBrush(Color.Black))
            using (Pen p = new Pen(Color.Black))
            {
                int x = 50;
                int y = e.MarginBounds.Top;
                e.Graphics.DrawRectangle(p, 10, 10, 400, 550);
                e.Graphics.DrawString("Your Elected Canditates are :", f, br, 15, 20);
                while (linesPrinted < _Candidates.Length)
                {
                    e.Graphics.DrawRectangle(p, x, y, 28, 28);
                    e.Graphics.DrawRectangle(p, x + 28, y, 300, 28);
                    e.Graphics.DrawString(_Candidates[linesPrinted].CandidateID.ToString(), f, br, x, y);
                    e.Graphics.DrawString(_Candidates[linesPrinted++].Name, f, br, x + 30, y);
                    y += 28;
                    if (y >= e.MarginBounds.Bottom)
                    {
                        e.HasMorePages = true;
                        return;
                    }
                    else
                    {
                        e.HasMorePages = false;
                    }
                }
            }
        }
    }
}